/*
 *  Copyright 2009 Lucas Nazário dos Santos
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.sourceforge.retriever.sample;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import net.sourceforge.retriever.Retriever;
import net.sourceforge.retriever.analyzer.Analyzer;
import net.sourceforge.retriever.executor.ThreadExecutor;
import net.sourceforge.retriever.fetcher.Resource;

/*
 * This sample shows how to change the number of threads used
 * by the crawler. The number of threads influence the number
 * of resources that can be crawled at once, as well as the amount
 * of bandwidth.
 */
public class DefiningTheNumberOfThreads {


	public static void main(final String[] args) throws MalformedURLException {

		final Retriever crawler = new Retriever();

		// Defines a pool of threads starting with one thread and growing up to two.
		// Also, defines a time of sixty seconds that threads will live before being killed.
		// It also specifies a queue that supports a maximum of ten requisitions. 
		crawler.setExecutor(new ThreadExecutor(1, 2, 60, TimeUnit.SECONDS, 10));

		// Adds a seed to the Google page
		crawler.addSeed(new URL("http://www.google.com/"));

		// Prints the URL of resources
		crawler.setAnalyzer(new Analyzer() {
			public void analyze(final Resource resource, final Map<String, Object> additionalInfo) {
				System.out.println(resource.getURL().toExternalForm());
			}
		});

		crawler.start();
	}
}